package com.gree.changying.activity.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;


/**
 * Create by 游德禄 on 2019-9-3
 */
public abstract class BasePageFragment extends BaseFragment {

    protected View mView;
    public BasePageActivity base ;

    protected boolean clearFragmentsTag() {
        return true;
    }
    private static final String BUNDLE_FRAGMENTS_KEY = "android:support:fragments";
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null && this.clearFragmentsTag()) {
            //重建时清除 fragment的状态
            savedInstanceState.remove(BUNDLE_FRAGMENTS_KEY);
        }
        super.onCreate(savedInstanceState);
        base = (BasePageActivity)getActivity();
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (outState != null && this.clearFragmentsTag()) {
            //销毁时不保存fragment的状态
            outState.remove(BUNDLE_FRAGMENTS_KEY);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater,container,savedInstanceState);
        if (mView == null) {
            mContext = getContext();
            mView = view.inflate(mContext, getLayoutId(), null);
            setContentView(mView);
            //NLog.e("sdgsdgsd","gsdsfsdfsdf");
            initView(mView);
            //NLog.e("sdgsdgsd","gsdsfsdfsdf1");
        } else {
            // 缓存的rootView需要判断是否已经被加过parent，如果有parent需要从parent删除，
            // 要不然会发生这个rootview已经有parent的错误。
            ViewGroup parent = (ViewGroup) mView.getParent();
            if (parent != null) {
                parent.removeView(mView);
            }
            //重载fragment
            mContext = getContext();
            mView = view.inflate(mContext, getLayoutId(), null);
            setContentView(mView);
            //NLog.e("sdgsdgsd","gsdsfsdfsdf");
            initView(mView);
        }
        return view;
    }

    protected abstract void initView(View view);

    protected abstract int getLayoutId();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    protected void initData() {
    }

    private boolean mIsHidden = false;
    private boolean mIsUserVisibleHint = true;
    //viewPager内第一次加载fragment，会执行setUserVisibleHint和onResume，字段防止onVisible（）执行两遍
    private boolean mIsExecuteOnVisible = false;
    //viewPager 预加载默认会执行一次setUserVisibleHint，初始第一次都为false，避免执行onHidden方法
    private boolean mIsFirstUserVisibleHint = true;
    /**
     * 目前只有在底部tab切换到viewPager内fragment时， onVisible（）不会执行，其余时机皆可
     */
    protected  void onVisible(){

    }

    protected  void onHidden(){

    }
    @Override
    public void onResume() {
        if (getUserVisibleHint() && !mIsHidden && !mIsExecuteOnVisible) {
            onVisible();
        }
        if (mIsExecuteOnVisible) {
            mIsExecuteOnVisible = false;
        }
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (!mIsHidden && mIsUserVisibleHint) {
            onHidden();
        }
        mIsExecuteOnVisible = false;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            onHidden();
        } else {
            onVisible();
        }
        mIsHidden = hidden;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        mIsUserVisibleHint = isVisibleToUser;
        if (isVisibleToUser) {
            if (!mIsExecuteOnVisible) {
                mIsExecuteOnVisible = true;
            }
            onVisible();
        } else {
            if (!mIsFirstUserVisibleHint) {
                onHidden();
            }
            mIsFirstUserVisibleHint = false;
        }
    }
}